<?php

namespace app\api\controller;

use app\admin\model\Pmsorderinfo;
use app\admin\model\Pmsroominfo;
use app\common\controller\Api;
use app\common\model\Category;
use services\QCloudSmsService;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * Description:
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: dtt
     * @date  : 2021/8/4 0004
     * @time  : 17:56
     */
    public function getDateMap()
    {
        $list = (new Pmsroominfo())->getRoomFullDate();//空房

        if (empty($list)) {
            $this->success('请求成功', []);
        }

        // 全部为锁房
        if ($list === 10001) {
            $this->success('请求成功', [], $list);
        }

        $this->success('请求成功', $list);
    }

    public function category()
    {
        $id  = $this->request->get('id');
        $pid = $this->request->get('pid');
        $map = [];
        if ($id) {
            $map['id'] = $id;
        }
        if ($pid) {
            $map['pid'] = $pid;
        }
        $this->success('获取成功', (new Category())->field('id,name,type,flag')->where($map)->select());
    }

    /**
     * Description: 模拟住房数据
     *
     * @return mixed
     * @author: dtt
     * @date  : 2021/8/4 0004
     * @time  : 17:55
     */
    public function seemRoomData()
    {
        for ($i = 0; $i < 5; $i++) {
            $num = rand(1, 10);
            for ($j = 1; $j <= $num - 1; $j++) {
                $stayList[$i][] = date('Y-m-d 00:00:00', strtotime("+$j day", strtotime("+$num day")));
            }
        }
        return $stayList;
    }

    public function sendLoginSms(string $mobile)
    {
        $code = rand(1000, 9999);
        session('captcha', $code);
        QCloudSmsService::create($mobile)->sendLoginSms($code);
        $this->success(__('发送成功！'));
    }

    public function checkInTimeTip()
    {
        $begin = strtotime(date('Y-m-d', strtotime('+1 day')));
        $end   = strtotime(date('Y-m-d 23:59:59', strtotime('+1 day')));
        $order = (new Pmsorderinfo())->where(['order_status' => -1])->whereTime('check_in_time', [$begin, $end])->select()->toArray();
        foreach ($order as $val) {
            QCloudSmsService::create($val['tel'])->sendStayHospitalSms($val['realname'], date('Y-m-d', $val['check_in_time']));
        }
    }

    public function checkOutTimeTwoDayTip()
    {
        $begin = strtotime(date('Y-m-d', strtotime('-2day')));
        $end   = strtotime(date('Y-m-d 23:59:59', strtotime('-2day')));
        $order = (new Pmsorderinfo())->where(['order_status' => 1])->whereBetween('check_out_time', [$begin, $end])->select()->toArray();
        foreach ($order as $val) {
            QCloudSmsService::create($val['tel'])->sendReturnVisitSms();
        }
    }

    public function checkStayDateAndCurDate()
    {
        $begin = strtotime(date('Y-m-d'));
        $order = (new Pmsorderinfo())->where(['order_status' => -1])->select();

        foreach ($order as $val) {
            if($begin >= $val['check_in_time']){
                $val['order_status'] = 0;
                $val->save();
            }
        }
    }
}
